<?php
/*
 * 还款 */
class repayModel
{
    /*
    本金还款记录：根据本金钱数，开始时间，结束时间，计算出来还款的记录
    每一条还款记录，之前都必须有三次刷卡记录：三个消费记录+一个还款记录 为一次。 每天不能超出9千；
    每笔刷卡（消费）不能超过1000 （<1000元）
    三次刷卡金额 == 一次还款金额 
    还款日期 - 还款金额 - 还款次数  ： 要判断这三个条件是否满足 每次刷卡不高于1000，每天还款不高于9000 的这个条件
    一天最多12次记录：代表最多一天还款三次（3刷 + 1还款）
  
        高远:
        是的   消费（刷卡）费率0.0075  手续费1元   x*1.0075+1 = 999 ; x = 990.5707196029777

        高远:
        还款费率0.0055  手续费2元
     *  money:还款金额
     * start_time:开始时间
     * end_time: 结束时间
     * cishu : 还款次数
     * $otherArr =  array('region'=>$region); //把地区传过来
         *          */
    public $max_per_consume;//单笔消费最大的本金；x*1.0075+1 = 999 ; x = 990.5707196029777 
    public $max_per_repaymoney; ////单笔最大还款金额
    public $consume_feilv; //消费费率
    public $sum_free ; //总费率消费的金额
//    public $ave_back = array(0.33,0.33,0.34); //还款的1元固定费率，分三次到刷卡里
    public function repay_consume_list($money,$start_time,$end_time,$cishu,$otherArr=array())
    {
        
        $max_day_money = 8999; //每天最大还款金额，每天还款金额不可以超过这个钱数
        $max_repay_money = 2999;//每笔最大还款金额
        
        $consume_feilv = 0.0075; //刷卡费率
        $consume_shouxufei = 1; //刷卡手续费1元
        $this->consume_feilv = $consume_feilv;
        $this->sum_free = $money * $consume_feilv; //总费率消费的金额
        
        $repay_feilv = 0.0055; //还款费率
        $repay_shouxufei = 2; //还款手续费2元
        
        if(isset($otherArr['day_max_repay']))
        {
            $max_day_money = $otherArr['day_max_repay']; //8999; //每天最大还款金额，每天还款金额不可以超过这个钱数
        }
        if(isset($otherArr['per_max_consume']))
        {
            $max_repay_money = $otherArr['per_max_consume'];// 2999;//每笔最大还款金额
        }
        if(isset($otherArr['consume_fenlv']))
        {
            $consume_feilv = $otherArr['consume_fenlv']; //0.0075; //刷卡费率
        }
        
        
        $max_per_consume = 900; //单笔消费最大的本金；x*1.0075+1 = 999 ; x = 990.5707196029777 
        $max_per_repaymoney = ($max_per_consume * (1+$consume_feilv) + $consume_shouxufei) * 3; //单笔最大还款金额
        $this->max_per_consume = $max_per_consume;
        $this->max_per_repaymoney = $max_per_repaymoney;
        if(date("H") > 22)
        {
            //如果当前时间超过22点，那么就从下一天开始计算
            $start_time = date("Y-m-d", strtotime("+1 day", strtotime("$start_time")));
        }
        
        $days = $this->diffBetweenTwoDays($start_time, $end_time);  //一共还款的总天数 天数相同的话，返回1天
        if(($days * 3) < $cishu)
        {
            $backarr = array('error'=>1,'msg'=>'还款天数过少');
            return json_encode($backarr,256);
        }
        $dayList = $this->_getdayList($start_time, $end_time); //总的还款日期列表
        if(count($dayList) > $cishu)
        {
            //天数大于次数的时候，随即选择还款记录
            $randday = array_rand($dayList, $cishu);
            $tmpArr = array();
            foreach($randday as $value)
            {
                if(isset($dayList[$value]))
                {
                    $tmpArr[$value] = $dayList[$value];
                }
            }
            $dayList = $tmpArr;
        }else
        {
            //$this->_day_repay_num($dayList, $cishu); //带入到每天还款多少此
        }
         
        $backrepayArr = array(); //打散后的还款金额
        if(isset($otherArr['repay_rand_List']))
        {
            $backrepayArr = $otherArr['repay_rand_List'];
        }else
        {
            $backrepayArr = $this->_getrepayList($money,$cishu); //打散后的还款金额
        }
         
        $this->_day_repay_num($dayList, $cishu); //带入到每天还款多少此
        
        
        $per_repaymoney = round(($money / $cishu),1); //平均每次 还款金额 四舍五入
        $end_repaymoney = $this->_get_end_money($money, $per_repaymoney, $cishu) ; //没有整除的时候，最后一次找平 还款金额
         
        $per_consume_money = round(($per_repaymoney / 3),1); //根据还款每次还款金额，可以判断出来三次消费金额：无费率和手续费 四舍五入
        $end_per_consume_money = $this->_get_end_money($per_repaymoney, $per_consume_money, 3);///最后一次找平
        $pd_consume_money = $this->_getcompare($per_consume_money, $end_per_consume_money); //获取最大消费数
//        echo "per_consume_money:{$per_consume_money},end_per_consume_money{$end_per_consume_money},pd_consume_money:{$pd_consume_money}";
        if($pd_consume_money > $max_per_consume)
        {
            $backarr = array('error'=>1,'msg'=>'消费超出最大消费记录');
            return json_encode($backarr,256);
        }
        //echo "per_repaymoney:{$per_repaymoney},end_repaymoney{$end_repaymoney}";
//        $aa = $this->_handle_repay(1666.7);
//        var_dump($aa); exit();
        
        $repayList = $this->_get_repay_List($dayList,$cishu,$per_repaymoney,$end_repaymoney,array('backrepayArr'=>$backrepayArr));//还款记录 

//        $repayconsumeList = $this->_get_repay_consume_List($repayList,(string)$per_consume_money,(string)$end_per_consume_money,$consume_feilv,$consume_shouxufei,$otherArr); //生成消费-还款记录
        $repayconsumeList = $this->_get_repay_consume_List($repayList,$consume_feilv,$otherArr); //生成消费-还款记录
//        echo json_encode($repayconsumeList,256); exit();
        
        
//        $data = array('per_repaymoney'=>$per_repaymoney,'end_repaymoney'=>$end_repaymoney);
//        $data['per_consume_money'] = $per_consume_money; //平均消费
//        $data['end_per_consume_money'] = $end_per_consume_money; //第三次的消费：找平消费和还款的
        $free = $money * $consume_feilv + $cishu;
        $data = array('error'=>0,'msg'=>"成功"); 
        $data['total_money'] = $money + $free;//总金额（本金+费率之和）
        $data['sumMoney'] = $money; //本金
        
        $data['free'] = $free;//综合费率综合
        $data['start_time'] = $start_time;
        $data['end_time'] = $end_time;
        $data['paytime'] = date("Y-m-d H:i:s");
        $data['region'] = $otherArr['region'];
        $data['mission_consume'] = $cishu * 3;
        $data['mission_repayment'] = $cishu; 
        $data['data'] = $repayconsumeList['list'];
        return $data;
    }
    
    //根据消费的金额，计算出 费率 + 手续费
    public function _consume_money($capital)
    {
        //消费（刷卡）费率0.0075  手续费1元
        $feilv = 0.0075;
        $shouxufei = 1;
        $fee = $capital * $feilv + $shouxufei;
        return $fee;
    }
    //根据消费金额，获取到费率，保留一位小数 
    public function _feilv_money($money)
    {
        $feilv = $this->consume_feilv;
        $fee = $money * $feilv ;
        $fee = round($fee,2);
        return $fee;
        $count = substr_count($fee,".");
        if($count > 0)
        {
            $feeArr = explode(".",$fee);
            $fee0 = $feeArr[0];
            $fee1 = $feeArr[1];
            $len = strlen($fee1);
            if($len >= 2)
            {
                $a1 = substr($fee1,0,1); //小数点后一位
                $a1 = $a1 + 1;
                if($a1 == 10)
                {
                    $fee = $fee0 + 1;
                }else
                {
                    $fee = $fee0.".".$a1;
                }
            } 
        }
        return $fee;
    }
    //没有整除的时候，最后一次找平  $money总数,  per_money平均数,   cishu次数
    public function _get_end_money($money,$per_money,$cishu)
    {
        $fee = $per_money * $cishu;
        if($fee > $money)
        {
            //说明多了，最后一次要减少下
            $diff = $fee - $money;
            $per_money = $per_money - $diff;
        }else
        {
            $diff = $money - $fee;
            $per_money = $per_money + $diff;
        }
        return $per_money;
    }
    
    public function _getcompare($a,$b,$act="max")
    {
        $renum = 0;
        if($act == "max")
        {
            //获取两个数的最大的数
            if($a >= $b)
            {
                $renum = $a;
            }else
            {
                $renum = $b;
            }
        }else
        {
            //获取两个数的最小的数
            if($a >= $b)
            {
                $renum = $b;
            }else
            {
                $renum = $a;
            }
        }
        return $renum;
    }
    //根据几天几次还款获取时间数组
    public function _day_back_numArr($cishu,$start_time,$end_time)
    {
        $backarr = array();//返回的数组
        $is_false_day = 0; //有几天是没有还款的
        $days = $this->diffBetweenTwoDays($start_time, $end_time);
        if($days > $cishu)
        {
            $is_false_day = $days - $cishu;
        }
        $days2 = $days - $is_false_day; //实际还款天数
//        $int_start_time = strtotime($start_time);
//        $daysArr = array();
//        for($i=0;$i<$days;$i++)
//        {
//            $time = strtotime("+{$i} day",$int_start_time);
//            $time = date("Y-m-d",$time);
//            $daysArr[] = $time;
//        }
//        echo "daysArr:";
//        var_dump($daysArr); exit();
        
        $per_day_repay_num = round(($cishu/$days),2); //平均每天还款次数
        $per_day_repay_num = (int)($cishu/$days); //平均每天还款次数
        $per_day_repay_num = ceil($cishu/$days); //平均每天还款次数
        if($per_day_repay_num == 0)
        {
            $per_day_repay_num = 1; //说明每天不到一次还款
        }
        $end_day_repay_num = $this->_get_end_money($cishu, $per_day_repay_num, $days2) ; //最后一天还款次数
//        echo "per_day_repay_num:{$per_day_repay_num},end_day_repay_num:{$end_day_repay_num}";
//        echo ",days:{$days},cishu:{$cishu}";
        if($per_day_repay_num > 3 || $end_day_repay_num > 3)
        {
            $backarr = array('error'=>1,'msg'=>'还款天数错误');
            return $backarr;
        }
        //9点到22点是还款时间，其他时间不还款：暂时先9点开始，半个小时一次（赶时间）
        if(empty($start_time))
        {
            $start_time = date("Y-m-d");
        }
        $time1 = $start_time." 09:30";
        $int_time1 = strtotime($time1); //开始时间戳，每次累积加1个小时  3600秒
        $index = 1;
        
//        echo "<br>days2:{$days2}<br>";
        for($i=1;$i<=$days2;$i++)
        { ///暂时先后几天不还款：递归判断轮空暂时没写对
            if($i < $days2)
            {
                ///前几天
                $s_time = $int_time1 + ($i-1)*86400;
                for($ii=1;$ii<=$per_day_repay_num;$ii++)
                {
                    //累加一个小时
                    $ss_time = $s_time + ($ii)*3600; //每次累加一个小时
                    $time = date("Y-m-d H:i:s",$ss_time);
                    $backarr[] = array('time'=>$time,'i'=>$index);
                    $index++;
                }
            }else
            {
                ///最后一天
                $s_time = $int_time1 + ($i-1)*86400;
                for($ii=1;$ii<=$end_day_repay_num;$ii++)
                {
                    //累加一个小时
                    $ss_time = $s_time + ($ii)*3600; //每次累加一个小时
                    $time = date("Y-m-d H:i:s",$ss_time);
                    $backarr[] = array('time'=>$time,'i'=>$index);
                    $index++;
                }
            }
        }
        return array('error'=>0,'data'=>$backarr);
    }
    
    /*
     * 两个日期之间相差的天数
    */
    public function diffBetweenTwoDays ($day1, $day2){
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return (($second1 - $second2) / 86400)+1;
    }
    //递归随机删除某一维数组元素 : num 随机删除几个元素
    public $Arr = array();
    public function _del_rand_array($array,$num)
    {
        $count = count($array);
        if($num>0)
        {
            if($array)
            {
                $x = rand(0,$count-1); 
                unset($array[$x]);
                $im = implode("||",$array);
                $tmpArr = explode("||",$im);
                $this->Arr = $tmpArr; 
                $this->_del_rand_array($tmpArr, $num-1);
            } 
        } 
    }
    //几次需要几天还：每天的还款次数
    public function _cishu($start_time,$end_time,$cishu,$capital)
    {
        $per_capital = (int)($capital / $cishu); //平均还款本金
        $end_per_capital = $this->_get_end_money($capital, $per_capital, $cishu); //最后一次还款本金
        
        $days = $this->diffBetweenTwoDays($start_time, $end_time); //需要几天还款
        
    }
    //返回还款日期列表
    public function _getdayList($start_time,$end_time)
    {
        $days = $this->diffBetweenTwoDays($start_time, $end_time); //需要几天还款
        $int_start_time = strtotime($start_time);
        $dayList = array();
        for($i=0;$i<$days;$i++)
        {
            $time = strtotime($start_time);
            $time2 = $time + $i*86400;
            $time3 = date("Y-m-d",$time2);
            $dayList[$time3] = array('time'=>$time3,"repay_num"=>0); //repay_num : 还款次数 
        }
        return $dayList;
    }
    //确定每天还款次数
    public $dayList = array();
    public function _day_repay_num(&$dayList,$cishu,$otherArr=array())
    {
        if($cishu == 0)
        {
            return $dayList;
        }
        $i = 0;
        foreach($dayList as $key=>$valArr)
        {
            if($cishu == 0)
            {
                break;
            }
//            echo $cishu;
            $repay_num = $valArr['repay_num'] + 1;
            $dayList[$key]['repay_num'] = $repay_num;
            $cishu--;
        }
//        $this->$dayList = $dayList;
        $this->_day_repay_num($dayList,$cishu);
    }
    //还款记录
    public function _get_repay_List($dayList,$cishu,$per_repaymoney,$end_repaymoney,$otherArr=array())
    {
        $repayList = array();
        $xx = 0;        
        
        $backrepayArr = $otherArr['backrepayArr']; //还款的打散后的数组
//        $per_repaymoney = current($backrepayArr);
//        $end_repaymoney = end($backrepayArr);
        foreach($dayList as $key=>$valArr)
        {
            $tmpArr = array();
            $repay_num = $valArr['repay_num']; //当天还款次数
            $time = $valArr['time'];
            if($cishu == 0)
            {
                return $repayList; 
            }
            
            for($x=1;$x<=$repay_num;$x++)
            {
                 
                $backmoney = $backrepayArr[$xx]; 
                $xx++;
                $time1 = $time." 09:00";
                $int_time = strtotime($time1);
                $int_time = $int_time + ($x)*3300 * 4;
                $tmpArr['time'] = date("Y-m-d H:i:s",$int_time);
//                if($cishu > 1)
//                {
//                    $tmpArr['repay_capital'] = $per_repaymoney; //平均还款金额
//                }else
//                {
//                    $tmpArr['repay_capital'] = $end_repaymoney; //最后一次还款金额：除不开的时候找平
//                }
                $tmpArr['repay_capital'] = $backmoney;
                $cishu--;
                $repayList[] = $tmpArr;
            } 
             
        }
        return $repayList;
    }
    
    public function _get_repay_consume_List($repayList,$consume_feilv,$otherArr)
    {
         
        $repayconsumeList = array();
        $sort = 1; //执行顺序
        $sum_sort = count($repayList); //总还款次数
        $total_money = 0; //总金额（本金+费率之和）
        $sumMoney = 0 ; //本金
        $free = 0; // 综合费率综合
        foreach($repayList as $key=>$valArr)
        {
            $tmpArr = array();
            $time = $valArr['time']; //还款的时间
            $repay_capital = $valArr['repay_capital'];//还款的金额
            $per_consume = (int)($repay_capital / 3); //平均消费金额
            $end_per_consume = $this->_get_end_money($repay_capital, $per_consume, 3);
//            echo "[repay_capital:{$repay_capital},per_consume:{$per_consume},end_per_consume:{$end_per_consume}]";
//            exit();
            $int_time = strtotime($time);
            $ii = 0;
            $_consumeList = array();
            $_consumeList = $this->_handle_repay($repay_capital); //根据还款金额，获取三次消费金额
            
//            echo "repay_capital:{$repay_capital}";
//            var_dump($_consumeList);
            for($i=1;$i<=3;$i++)
            {
                $space = 1800 ; //消费间隔
                $int_time2 = $int_time - ((4-$i) * $space) - rand(100,300) ; //5分钟刷一次消费
                $time2 = date("Y-m-d H:i:s",$int_time2);
                $consume = $per_consume;
                if($i==3)
                {
                    $consume = $end_per_consume;
                }
                $consume = $_consumeList[$ii];
                $ii++;
//                $feilv_fee = $consume * $consume_feilv; 
//                $feilv_fee = ceil($consume * $consume_feilv);
                $feilv_fee = $this->_feilv_money($consume);
                $this->sum_free = $this->sum_free - $feilv_fee; ///剩余的总费率。不能超过这个总费率
                if($this->sum_free < 0)
                {
                    $feilv_fee = $feilv_fee + $this->sum_free;
                    $this->sum_free = 0;
                }
                if($sort == $sum_sort)
                {
                    //最后一次还款
                    if($this->sum_free > 0) //说明sum_free 还有剩余，都加入到最后一次，找平
                    {
                        $feilv_fee = $feilv_fee + $this->sum_free;
                    }
                }
                
                
                $sum_consume = $consume + $feilv_fee;
                //time:刷卡时间，consume:消费本金，fenlv_fee:费率+手续费，sum_consume:总消费 ,repay_capital:还款金额
//                $tmpArr[] = array('day'=>date("Y-m-d",$int_time),'time'=>$time2,'consume'=>$sub_consume,
//                    'feilv_fee'=>$feilv_fee,'sum_consume'=>$consume,'repay_capital'=>0);
                $tmpArr[] = array(
                    'money'=>$consume,
                    'time'=>$time2,
                    'type'=>2,
                    'sort'=>$sort,
                    'fee'=>$feilv_fee,
                    'sum_money'=>$sum_consume,
                    'mcc'=>$otherArr['region'],
                    'mcc_name'=>$otherArr['region']
                );
                $free = $free + $feilv_fee;
            }
            //time:刷卡时间，consume:消费本金，fenlv_fee:费率+手续费，sum_consume:总消费 ,repay_capital:还款金额
//            $tmpArr[] = array('day'=>date("Y-m-d",$int_time),'time'=>$time,'consume'=>0,'feilv_fee'=>0,
//                'sum_consume'=>0,'repay_capital'=>$repay_capital);
            $tmpArr[] = array(
                'money'=>$repay_capital,
                'time'=>$time,
                'type'=>1,
                'sort'=>$sort,
                'fee'=>0,
                'mcc'=>$otherArr['region'],
                'mcc_name'=>$otherArr['region']
            );
            $repayconsumeList[] = $tmpArr;
            $sort++;
        }
        $backarr = array(
            'list'=>$repayconsumeList,
            'free'=>$free,
        );
        return $backarr;
    }
    ///初始化随即时间
    public function _reset_time_List($repayconsumeList)
    {
        $backarr = array();
        foreach($repayconsumeList as $key=>$valArr)
        {
            foreach($valArr as $key2=>$valArr2)
            {
                $day = $valArr2['day'];
//                echo $day; echo "<br>";
            }
        }
        return $backarr;
    }
    //保留小数点一位小数，并进一  0.33 = 0.4
    public function _baoliu($number)
    {        
        $number = round($number,2);
        $numberArr = explode(".",$number);
        $z = (int)$numberArr[0];
        if(isset($numberArr[1]))
        {
            $len = strlen($numberArr[1]);
            if($len == 2)
            {
                $s1 = substr($numberArr[1], 0, 1);
                $s1 = (int)$s1 + 1;
            }
            if($s1 == 10)
            {
                $number = $z + 1;
            }else
            {
                $number = $z.".".$s1;
            }
        }
        return $number;    
    }
    //根据金额，还款次数，随机获取还款金额
    public function _getrepaymoneyList($money,$cishu)
    {
        $max_per_repaymoney = $this->max_per_repaymoney; ///最大的还款金额。因为有最大消费金额所以可以确定最大还款金额
        $ave = (int)($money/$cishu * 100) / 100;
        
        echo $max_per_repaymoney; echo "<br>";
        echo $ave;
        if($max_per_repaymoney >= $ave)
        {
            $backarr = array('error'=>0,'msg'=>'成功。');
            $data = array();  
            $total_money=$money;
            $total_num=$cishu;
            $total_money=$total_money - $total_num;
            for($i=$total_num;$i>0;$i--){
                $data[$i]=1;
                $ls_money=0;
                if($total_money>0){
                    if($i==1){
                        $data[$i] +=$total_money;
                    }else{
                        $max_money=floor($total_money/$i);
                        $ls_money=mt_rand(0,$max_money);
                        $data[$i]+=$ls_money;
                    }
                }
                $total_money -= $ls_money;
            }
            
            $backarr['data'] = $data;
            $this->_handleArr($data);
            return $backarr;
        }else
        {
            return array('error'=>1,'msg'=>'超出消费最大值。');
        }
    }
    //根据还款金额，获取三次消费金额
    public function _getconsumeList($money,$cishu=3)
    {
        $max = $this->max_per_consume; ///最大的消费金额
        $ave = (int)($money/$cishu * 100) / 100;
        
        echo $max; echo "<br>";
        echo $ave;
        if($max >= $ave)
        {
            $backarr = array('error'=>0,'msg'=>'成功。');
            $data = array(); //三次消费的记录
            $total_money=$money;
            $total_num=$cishu;
            $total_money=$total_money - $total_num;
            for($i=$total_num;$i>0;$i--){
                $data[$i]=1;
                $ls_money=0;
                if($total_money>0){
                    if($i==1){
                        $data[$i] +=$total_money;
                    }else{
                        $max_money=floor($total_money/$i);
                        $ls_money=mt_rand(0,$max_money);
                        $data[$i]+=$ls_money;
                    }
                }
                $total_money -= $ls_money;
            }
            
            $backarr['data'] = $data;
            $this->_handleArr($data);
            return $backarr;
        }else
        {
            return array('error'=>1,'msg'=>'超出消费最大值。');
        }
    }
    public function _handleArr($data)
    {
        echo "<hr>";
        $max_per_repaymoney = $this->max_per_repaymoney; ///最大的还款金额。因为有最大消费金额所以可以确定最大还款金额
        $Arr = array();
        foreach($data as $key=>$val)
        {
           $Arr[] = $val;
        }
        $newArr = array();
        $duoyu = 0 ; //记录多余的
        
    }
    public function _digui(&$Arr)
    {
        $max_per_repaymoney = $this->max_per_repaymoney; ///最大的还款金额。因为有最大消费金额所以可以确定最大还款金额
        $duoyu = 0 ; //记录多余的
        foreach($Arr as $key=>$value)
        {
            if($value > $max_per_repaymoney)
            {
                $duoyu = $value - $max_per_repaymoney;
                $Arr[$key] = $max_per_repaymoney;
            }
        }
    }
    //根据还款金额生成三个消费金额
    public function _handle_repay($repaymoney)
    {
        $ave_consume = (int)($repaymoney/3 );
        $max_per_repaymoney = $this->max_per_repaymoney; ///最大的还款金额。因为有最大消费金额所以可以确定最大还款金额
        $ave_max_consume = (int)($max_per_repaymoney/3 * 100)/100;
        $end_ave_consume = $this->_get_end_money($repaymoney, $ave_consume, 3);
        $cha = $ave_max_consume - $ave_consume;
//        echo "[repaymoney:{$repaymoney},cha:{$cha},ave_consume:{$ave_consume},end_ave_consume:{$end_ave_consume}]";
        if($cha >80)
        {
            $rand = rand(0,30);
            $rand = $rand + 50;
            $a1 = $ave_consume - $rand;
            $a2 = $ave_consume + $rand;
            $a3 = $end_ave_consume;
            
            $a3 = $ave_consume + 80;
            $a_shengyu = $repaymoney - $a3;
            $a_2 = $a_shengyu / 2; //剩余的平均数
            if($a_2 > 100)
            {
                $aa = rand(100,$a_2);
                $a2 = $aa;
                $a1 = $a_shengyu - $a2;
            }else
            {
                $rand2 = rand(1,30);
                $a2 = $a_2 + $rand2;
                $a1 = $a_shengyu - $a2;
            }
            $Arr = array($a1,$a2,$a3);
        }else
        {
            $Arr = array($ave_consume,$ave_consume,$end_ave_consume);
        }
        shuffle($Arr);
        return $Arr;
    }
    
    public function _getrepayList($money,$cishu)
    {
        
        $max_per_repaymoney = $this->max_per_repaymoney; ///最大的还款金额。因为有最大消费金额所以可以确定最大还款金额
        $ave_max_repaymoney = (int)($max_per_repaymoney/3 * 100)/100; 
        $ave_repaymoney = (int)($money/$cishu);
        $end_ave_repaymoney = $this->_get_end_money($money, $ave_repaymoney, $cishu);
        $backarr = array();
        //echo "money:{$money},cishu:{$cishu},ave_repaymoney:{$ave_repaymoney},end_ave_repaymoney:{$end_ave_repaymoney}";
        for($i=1;$i<=$cishu;$i++)
        {
            if($i < $cishu)
            {
                $backarr[] = $ave_repaymoney;
            }else
            {
                $backarr[] = $end_ave_repaymoney;
            }
        }
        return $backarr;
    }
    /**
     * [计划生成]
     * @Author tw
     * @param  [type] $money     [金额]
     * @param  [type] $num       [笔数]
     * @param  [type] $money_min [最小金额]
     * @param  [type] $money_max [最大金额]
     * @param  string $min       [description]
     * @param  string $max       [description]
     * @param  string $decimals  [是否有小数]
     * @return [type]            [description]
     */
    function randnum_new($money,$num,$min_money,$max_money,$min_risk,$max_risk, $decimals= 'false'){
    	//halt($max_risk);
        $average = round($money / $num);
        $risk_min_money = $average - $min_money;//风控最小值
        // $max_risk = $max_money - $average;

        $sum = 0;
        $result = array_fill( 1, $num, 0 );
        for( $i = 1; $i < $num; $i++ ){
            $risk = round( (rand( $min_risk, $max_risk )) / 2 );
            if($risk > $risk_min_money)
            {
                $risk = $risk_min_money/2;
            }
            if( $sum > 0 ){
                $max = 0;
                $min = 0 - $risk;
            }elseif( $sum < 0 ){
                $min = 0;
                $max = $risk;
            }else{
                $max = $risk;
                $min = 0 - $risk;
            }
            $random = rand( $min, $max );//+rand(-999,999)

            if($decimals)
            {
                $random = $random.".".rand(0,9);
            }
            $sum += $random;
            $result[$i] = toFixed($average + $random);
            if($result[$i] < $min_money)
            {
                $result[$i] = $min_money;
            }
            elseif($result[$i]>$max_money)
            {
                $result[$i] = $max_money;
            }
        }
     
        //最后一份的份额由前面的结果决定，以保证各份的总和为指定值
        $result[$num] = toFixed($money - array_sum($result));
        foreach( $result as $temp ){
            $data[] = $temp;
        }
        //halt($data);
        return $data;
    }
    
    public function _add_sort($dayList,$otherArr=array())
    {
        $sort = 1;
        foreach($dayList as $key=>$value)
        {
            $dayList[$key]['sort'] = $sort;
            $sort++;
        }
        return $dayList;
    }
}
?>